<template>
  <view class="container">
    <view v-if="userInfo">
      <text>欢迎，{{ userInfo.username }}</text>
      <button @click="logout">退出登录</button>
    </view>
    <view v-else>
      <text>加载中...</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      userInfo: null
    };
  },
  onLoad() {
    this.getUserInfo();
  },
  methods: {
    getUserInfo() {
      uni.$u.api.uniIdGetUserInfo().then(res => {
        if (res.data.code === 0) {
          this.userInfo = res.data.data;
        }
      });
    },
    logout() {
      uni.$u.api.uniIdLogout().then(res => {
        if (res.data.code === 0) {
          uni.showToast({ title: '退出成功' });
          uni.switchTab({ url: '/pages/member/member' });
        }
      });
    }
  }
};
</script>